package com.yam.michael.hibernate.dao.impl;

import java.io.Serializable;
import java.sql.SQLException;
import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.xunjia.exception.XunjiaException;
import com.yam.michael.hibernate.dao.AdvPositionDao;
import com.yam.michael.hibernate.pojo.AdvPosition;

public class AdvPositionDaoImpl extends HibernateDaoSupport implements AdvPositionDao,Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = -1050802658618627411L;
	//日志
	private static final Logger logger = Logger.getLogger(AdvPositionDaoImpl.class); 

	@Override
	/**
	 * 获取广告位置信息
	 * @return
	 * @throws XunjiaException
	 */
	public AdvPosition getAdvPositionDaoById(String position) throws XunjiaException {
		try {
			return (AdvPosition) this.getHibernateTemplate().get(AdvPosition.class, position);
		} catch (Exception e) {
			logger.info(e.getMessage());
			throw new XunjiaException("获取广告位信息发生错误,获取失败!!!!");
		}
	}

	@SuppressWarnings("unchecked")
	@Transactional(isolation = Isolation.SERIALIZABLE, propagation = Propagation.REQUIRED, rollbackFor = { XunjiaException.class })
	@Override
	public List<AdvPosition> getAdvPosition() throws XunjiaException {
		return (List<AdvPosition>) this.getHibernateTemplate().execute(new HibernateCallback(){
			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query q = session.createQuery("FROM AdvPosition ");
				return q.list();
			}});
	}
	
}
